package com.tiancheng.trade.payment.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.BusinessException;
import com.tiancheng.trade.commom.web.model.request.RefundVerificationRequestVO;
import com.tiancheng.trade.payment.channel.bo.*;
import com.tiancheng.trade.payment.enums.AppleNotificationTypeEnum;
import com.tiancheng.trade.payment.model.PaymentRefundFlow;
import com.tiancheng.trade.payment.vo.admin.PaymentRefundFlowQueryVo;
import com.tiancheng.trade.payment.vo.admin.res.PaymentRefundQueryResVO;
import com.tiancheng.trade.payment.vo.api.RefundOrderVO;
import com.tiancheng.trade.commom.web.model.response.RefundOrderResVO;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * 退款
 */
public interface PaymentRefundService extends IService<PaymentRefundFlow> {

    /**
     * 退款
     *
     * @param refundOrderVO
     * @return
     * @throws BusinessException
     */
    RefundOrderResVO refund(RefundOrderVO refundOrderVO) throws BusinessException;
    /**
     * 抵扣单退款
     *
     * @param refundOrderVO
     * @return
     * @throws BusinessException
     */
    RefundOrderResVO deductionRefund(RefundOrderVO refundOrderVO) throws BusinessException;

    /**
     * 根据退款单号查询
     *
     * @param flowId
     * @return
     */
    PaymentRefundFlow findByFlowId(String flowId);

    /**
     * 根据id获取支付流水
     *
     * @param id
     * @return
     */
    PaymentRefundFlow get(Long id);

    /**
     * 根据id获取退款流水详情
     *
     * @param id
     * @return
     */
    PaymentRefundQueryResVO getInfo(Long id);

    /**
     * 添加支付流水
     *
     * @param paymentRefundFlow
     * @return
     */
    long add(PaymentRefundFlow paymentRefundFlow);


    /**
     * 根据退款订单号获取退退款记录
     *
     * @param paymentId 退款订单号
     * @return
     */
    PaymentRefundFlow findByPaymentId(String paymentId);

    /**
     * 分页查询退款
     *
     * @param vo
     * @return
     */
    PageInfo findList(PaymentRefundFlowQueryVo vo);

    /**
     * 退款结果
     *
     * @param refundResultBO
     * @return
     * @throws BusinessException
     */
    PaymentRefundFlow refundResult(RefundResultBO refundResultBO) throws BusinessException;

    /**
     * 根据商户查询应清分退款流水
     *
     * @param merchants {@link Collection<String>}
     * @return {@link List<RefundSettleBO>}
     */
    List<RefundSettleBO> listSettleRefund(Collection<String> merchants);

    /**
     * 查询清分支付流水对应的当日退款成功退款流水
     *
     * @param flowDetailIds
     * @return
     */
    List<RefundSettleBO> listApprRefund(List<Long> flowDetailIds);

    /**
     * 退款流水明细批量更新-所更新字段值相同
     *
     * @param ids       id
     * @param colValMap 字段-值：(column, value)
     * @return
     */
    Integer updateSettleBatch(List<Long> ids, HashMap<String, Object> colValMap);

    // 导出excel
    Result exportList(PaymentRefundFlowQueryVo vo, HttpServletResponse response) throws IOException;

    /**
     * 根据商户查询应清分退款优惠流水
     *
     * @param expectSettleTime 优惠清分执行时间
     * @return {@link List<RefundSettleBO>}
     */
    List<RefundCouponSettleBO> queryCouponSettleRefund(LocalDateTime expectSettleTime);

    /**
     * 苹果退款回调
     *
     * @param signedTransactionInfo 退款参数
     * @param refundType            退款类型
     */
    RefundResultBO appleRefund(AppleSignedTransactionInfo signedTransactionInfo, AppleNotificationTypeEnum refundType);

    /**
     * 查询退款状态<br></br>
     * 1、查询本地数据库状态
     * 2、如果本地数据库状态为退款中、查询银行渠道退款状态
     * @param refundFlowId 退款流水号
     */
    Result<GetRefundOrderResBO>  queryRefundStatus(String refundFlowId);

    /**
     * 退款订单核销
     * @param refundVerificationRequestVO
     */
    void verification(RefundVerificationRequestVO refundVerificationRequestVO);
}
